#ifndef SPEECH_H
#define SPEECH_H

#include <QObject>
#include <QTextToSpeech>
#include <QDebug>

class GameClientModel;
class L_Speech: public QObject
{
    Q_OBJECT
public:
    explicit L_Speech(QObject *parent = nullptr);
    virtual ~L_Speech();

public:
    int engineSelect(QString engineName)
    {
        if(m_Speech != nullptr)
            delete m_Speech;

        if (engineName == "default")
            m_Speech = new QTextToSpeech(this);
        else
            m_Speech = new QTextToSpeech(engineName, this);

        return 0;
    }

    int locateSelect(int index)
    {
        QVector<QLocale> locales = m_Speech->availableLocales();
        /*
        QLocale current = m_Speech->locale();
        foreach (const QLocale &locale, locales) {
            QString name(QString("%1 (%2)")
                         .arg(QLocale::languageToString(locale.language()))
                         .arg(QLocale::countryToString(locale.country())));
            QVariant localeVariant(locale);
            ui.language->addItem(name, localeVariant);
            if (locale.name() == current.name())
                current = locale;
        }*/

        if(index >= locales.length())
            return -1;

        QLocale locale = locales.at(index);
        m_Speech->setLocale(locale);

        QString name(QString("%1 (%2)")
                     .arg(QLocale::languageToString(locale.language()))
                     .arg(QLocale::countryToString(locale.country())));
        qDebug() << name;

        return 0;
    }

    int voiceSelect(int index)
    {
        QVector<QVoice> m_voices = m_Speech->availableVoices();
        /*
        QVoice currentVoice = m_Speech->voice();
        foreach (const QVoice &voice, m_voices) {
            ui.voice->addItem(QString("%1 - %2 - %3").arg(voice.name())
                              .arg(QVoice::genderName(voice.gender()))
                              .arg(QVoice::ageName(voice.age())));
            if (voice.name() == currentVoice.name())
                ui.voice->setCurrentIndex(ui.voice->count() - 1);
        }
        connect(ui.voice, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);
        */

        if(index >= m_voices.length())
            return -1;

        QVoice voice = m_voices.at(index);
        m_Speech->setVoice(voice);

        QString name(QString("%1 - %2 - %3").arg(voice.name())
                     .arg(QVoice::genderName(voice.gender()))
                     .arg(QVoice::ageName(voice.age())));
        qDebug() << name;

        return 0;
    }

    int read(QString text)
    {
        if(m_Speech->state()==QTextToSpeech::Ready)
        {
            m_Speech->say(text);
            return 0;
        }
        else
            return -1;
    }

public:
    QTextToSpeech *m_Speech;

protected:

};

#endif // SPEECH_H
